<?php
require_once("database.php");
require_once("util.php");

function questions_POST($id)
{
	global $picture_path;
	global $api_uri;
	$db = getDBConnection();
	
	if(isset($_FILES['Filedata']['tmp_name']))
	{	
		// Preview image was uploaded
		$qid = getUniqueQuestionID($id, $db);
		
		$tmp_file_name = $_FILES['Filedata']['tmp_name'];
		$original_name = $_FILES['Filedata']['name'];
		$extension = getFileExtension($original_name);
		$new_file_name = $picture_path.$id."_".$qid.$extension;
		
		$ok = move_uploaded_file($tmp_file_name, $new_file_name);
		
		if(!$ok)
		{
			printErrorCode(500);
		}
		
		$uri = $api_uri.$new_file_name;
		
		$arr = array("qid" => $qid, "uri" => $uri);
		
		return $arr;
	}else
	{
		$data = json_decode(file_get_contents('php://input'));
		$extension = getFileExtension($data->uri);
		$uri = $data->id."_".$data->qid.$extension;
		$opened = ($data->qid == 1) ? 1 : 0;
		$sql = "INSERT INTO question VALUES('".$data->qid."', '".$data->id."', '".$data->title."', '".$uri."', '".$data->type."', '".$data->answers."', '".$data->correct_answers."', '".$opened."')";
		mysqli_query($db, $sql);
		
		$arr = array("qid" => $data->qid);
		return $arr;
	}
}

function getUniqueQuestionID($id, $db)
{
	// Only select the highest ID.
	$sql = "SELECT qid FROM question WHERE quiz_id = '".$id."' ORDER BY qid desc LIMIT 1";
	$result = mysqli_query($db, $sql);
	
	if(mysqli_num_rows($result) == 0)
	{
		// First question -> ID = 1
		return 1;
	}else
	{
		// Get highest ID and add 1
		$row = mysqli_fetch_array($result);
		$id = $row['qid'] + 1;
		
		return $id;
	}	
}

function questions_GET($id)
{
	global $api_uri;
	global $picture_path;
	
	$db = getDBConnection();
	
	$sql = "SELECT * FROM question WHERE quiz_id = '".$id."' ORDER BY qid asc";
	$result = mysqli_query($db, $sql);
	$question_data = array();
	
	$count = 0;
	if(($count = mysqli_num_rows($result)) > 0)
	{
		for($i = 0; $i < $count; $i++)
		{
			$row = mysqli_fetch_assoc($result);
			$question_data[$i]["qid"] = $row["qid"];
			$question_data[$i]["id"] = $row["quiz_id"];
			$question_data[$i]["title"] = $row["title"];
			$question_data[$i]["uri"] = $row["image_uri"];
			$question_data[$i]["type"] = $row["type"];
			$question_data[$i]["answers"] = $row["answers"];
			$question_data[$i]["correct_answers"] = $row["correct_answers"];
			$question_data[$i]["opened"] = $row["opened"];
			$question_data[$i]["image_uri"] = $api_uri.$picture_path.$row["image_uri"];
		}
	}else
	{
		printErrorCode(404);
	}
	
	return $question_data;
}

function questions_PUT($id)
{
	$db = getDBConnection();	
	$arr = json_decode(file_get_contents('php://input'));
	
	if(!$arr)
	{
		return 0;
	}
	
	for($i = 0; $i < count($arr); $i++)
	{
		$data = $arr[$i];
		
		$extension = getFileExtension($data->uri);
		$uri = $data->id."_".$data->qid.$extension;
		$qid = $data->qid;
		
		$sql = "UPDATE question SET "
			  ."title = '".$data->title."', "
			  ."image_uri = '".$uri."', "
			  ."type = '".$data->type."', "
			  ."answers = '".$data->answers."', "
			  ."correct_answers = '".$data->correct_answers."', "
			  ."opened = '".$data->opened."' "
			  ."WHERE qid = '".$qid."' AND quiz_id = '".$id."'";
		mysqli_query($db, $sql);
	}
	
}

function question_GET($id, $qid)
{
	global $api_uri;
	global $picture_path;

	$db = getDBConnection();	
	$question_data = array();
	
	$sql = "SELECT * FROM question WHERE qid = '".$qid."' AND quiz_id = '".$id."' LIMIT 1";
	$data = mysqli_query($db, $sql);
	
	if(mysqli_num_rows($data) > 0)
	{
		$row = mysqli_fetch_assoc($data);
		
		$question_data["qid"] = $row["qid"];
		$question_data["id"] = $row["quiz_id"];
		$question_data["title"] = $row["title"];
		$question_data["uri"] = $row["image_uri"];
		$question_data["type"] = $row["type"];
		$question_data["answers"] = $row["answers"];
		$question_data["correct_answers"] = $row["correct_answers"];
		$question_data["opened"] = $row["opened"];
		$question_data["image_uri"] = $api_uri.$picture_path.$row["image_uri"];
		
	}else
	{
		printErrorCode(404);
	}

	return $question_data;
}

function question_PUT($id, $qid)
{
	$db = getDBConnection();	
	$data = json_decode(file_get_contents('php://input'));
	
	if(!$data)
	{
		return 0;
	}
	
	$extension = getFileExtension($data->uri);
	$uri = $data->id."_".$data->qid.$extension;
	
	$sql = "UPDATE question SET "
		  ."title = '".$data->title."', "
		  ."image_uri = '".$uri."', "
		  ."type = '".$data->type."', "
		  ."answers = '".$data->answers."', "
		  ."correct_answers = '".$data->correct_answers."', "
		  ."opened = '".$data->opened."' "
		  ."WHERE qid = '".$qid."' AND quiz_id = '".$id."'";
	mysqli_query($db, $sql);
	

	return 0;
}

function question_DELETE($id, $qid)
{
	$db = getDBConnection();	
	
	$sql = "DELETE FROM question WHERE qid = '".$qid."' AND quiz_id = '".$id."'";
	mysqli_query($db, $sql);

	return 0;
}

?>